<?php

namespace app\controllers;

use app\common\widgets\Tools;
use app\models\AdminModel;
use app\models\dao\Admin;
use Yii;
use yii\filters\AccessControl;
use yii\web\Response;
use yii\filters\VerbFilter;
use app\models\LoginForm;
use app\models\ContactForm;

class SiteController extends BaseController
{
    /**
     * @inheritdoc
     */
    public function actions()
    {
        return [
            'error' => [
                'class' => 'app\common\ErrorHandler',
            ]
        ];
    }

    /**
     * Displays homepage.
     *
     * @return string
     */
    public function actionIndex()
    {
        return $this->render('index');
    }

    public function actionLogin() {
        // echo "<pre>";print_r(111);exit;
        $this->view->params['tpl_class'] = 'bg-info dker';
        $csrf = \Yii::$app->request->csrfToken;
        // echo "222";exit;
        if (!\Yii::$app->user->isGuest) {
            return $this->goHome();
        }

        $model = new LoginForm();
//        echo "<pre>";print_r($model->load(Yii::$app->request->post()));exit;
        if($model->load(Yii::$app->request->post())) {
            $loginForm = Yii::$app->request->post();
            $post_csrf = $loginForm['_csrf'];
            // if($post_csrf != $csrf) {
            //     Tools::sendJson(410000110,"非法请求!");
            // }
            $username = trim($loginForm['LoginForm']['username']);
            $password = $loginForm['LoginForm']['password'];
            if(empty($username)) {
                Tools::sendJson(410000108,"管理员账号不能为空!");
            }
            if(empty($password)) {
                Tools::sendJson(410000109,"管理员密码不能为空!");
            }
            $result_login = $model->login();

            if($result_login) {
                //登陆成功
                // return $this->redirect(\Yii::$app->urlManager->createUrl(['site/index']));
                Tools::sendJson(0,"登陆成功");
            } else {
                //登陆失败
                Tools::sendJson(410000101,"用户名或密码错误");

            }
        } else {
            return $this->render('login', [
                'model' => $model,
            ]);
        }
    }

    /**
     * Logout action.
     *
     * @return Response
     */
    public function actionLogout() {
        Yii::$app->user->logout();
        return $this->redirect(['site/login']);
    }

    /**
     * Displays contact page.
     *
     * @return Response|string
     */
    public function actionContact()
    {
        $model = new ContactForm();
        if ($model->load(Yii::$app->request->post()) && $model->contact(Yii::$app->params['adminEmail'])) {
            Yii::$app->session->setFlash('contactFormSubmitted');

            return $this->refresh();
        }
        return $this->render('contact', [
            'model' => $model,
        ]);
    }

    /**
     * Displays about page.
     *
     * @return string
     */
    public function actionAbout()
    {
        return $this->render('about');
    }
}
